package com.logic.idevice.impl.common

import android.content.Context
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import com.logic.idevice.IUHFSetting
import com.myutils.UHFHelp
import com.myutils.UHFUtils
import com.sinopec.basemodule.BaseApplication
import com.sinopec.basemodule.constants.PreferencesConstant
import kotlin.experimental.and

/**
 * create by lwj at 2022/7/13
 * 设备：Common
 */
class CommonSetting(
    val refreshPowerUi:(Int)->Unit,
    val refreshTemUi:(String)->Unit,
) : IUHFSetting {

    private lateinit var mUhfHelp: UHFHelp

    override fun init(context: Context, lifecycle: Lifecycle) {
        UHFHelp.newInstance().initConnectUtils(BaseApplication.getInstance())
        mUhfHelp = UHFHelp.newInstance()
        mUhfHelp.initUHF()
        mUhfHelp.addUhfSetReceiver(context)

        lifecycle.addObserver(LifecycleEventObserver{ source, event ->
            when(event){
                Lifecycle.Event.ON_RESUME ->{
                    mUhfHelp.onResume()
                }
                Lifecycle.Event.ON_DESTROY ->{
                    mUhfHelp.unregisterUhfSetReceiver(context)
                }
            }
        })

        mUhfHelp.setSetCallback(object : UHFUtils.SetCallback {
            override fun refreshPower() {
                //刷新功率
                refreshPowerUi(this@CommonSetting.readPower())
            }

            override fun refreshTemperature() {
                //刷新温度
                refreshTemUi(this@CommonSetting.readTemperature())
            }
        })
    }

    override fun openVoince(): Boolean {
        return PreferencesConstant.getInstance().beeper_model == 1
    }

    override fun save(
        progress: Int,
        openVoince: Boolean
    ) {
        //1.设置功率

        //1.设置功率
        val progressB: Byte = progress.toByte()
        mUhfHelp.getReader().setOutputPower(UHFHelp.getM_curReaderSetting().btReadId, progressB)
        UHFHelp.getM_curReaderSetting().btAryOutputPower = byteArrayOf(progressB)
        //2.保存提示音状态
        //2.保存提示音状态
        PreferencesConstant.getInstance().beeper_model = if (openVoince) 1 else 0
        //3.保存盘询参数
//        val sessionIndex: Int = mSessionStrSpinner.getSelectedItemPosition()
//        val flagIndex: Int = mFlagStrSpinner.getSelectedItemPosition()
//        setSessionAndTarget(sessionIndex, flagIndex)


    }

    override fun readTemperature(): String {
        return if (UHFHelp.getM_curReaderSetting().btPlusMinus.toInt() == 0x0) {
            "-" + (UHFHelp.getM_curReaderSetting().btTemperature and 0xFF.toByte()) + "°C"
        } else {
            (UHFHelp.getM_curReaderSetting().btTemperature and 0xFF.toByte()).toString() + "°C"
        }
    }

    override fun refreshTemperature() {
        mUhfHelp.getReader().getReaderTemperature(UHFHelp.getM_curReaderSetting().btReadId);
    }

    override fun readPower(): Int {
        return if (UHFHelp.getM_curReaderSetting().btAryOutputPower != null) {
            (UHFHelp.getM_curReaderSetting().btAryOutputPower[0] and 0xFF.toByte()).toInt()
        } else {
            0
        }
    }

    override fun refreshPower() {
        mUhfHelp.getReader().getOutputPower(UHFHelp.getM_curReaderSetting().btReadId);
    }
}